layui.use(['table', 'admin', 'ax', 'form', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    var leaveInfo = {
        tableId: "leaveInfoTable"
    };

    /**
     * 初始化表格的列
     */
    leaveInfo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'leaveInfoId', align: "center", hide: true, title: '请假信息ID'},
            {field: 'leaveUserName', align: "center", sort: false, title: '请假人'},
            {field: 'leaveInfoTypeText', align: "center", sort: false, title: '请假类型'},
            {field: 'leaveInfoReason', align: "center", sort: false, title: '请假事由'},
            {field: 'leaveInfoStart', align: "center", sort: false, title: '开始时间'},
            {field: 'leaveInfoEnd', align: "center", sort: false, title: '结束时间'},
            {field: 'leaveInfoAffix', align: "center", sort: false, title: '位置'},
            {field: 'leaveInfoStatusText', align: "center", sort: false, title: '当前状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    leaveInfo.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['leaveFlag'] = $("#leaveFlag").val();
        table.reload(leaveInfo.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 导出excel按钮
     */
    leaveInfo.exportExcel = function () {
        var checkRows = table.checkStatus(leaveInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    leaveInfo.openDetailDlg = function (data) {
        func.open({
            height: 600,
            title: '查看请假详情信息',
            content: Feng.ctxPath + '/leaveInfo/leaveInfo_detail?leaveInfoId=' + data.leaveInfoId,
            tableId: leaveInfo.tableId
        });
    };

    /**
     * 销假
     */
    leaveInfo.onDestroyItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/leaveInfo/destroy", function (data) {
                if (data.code == 200) {
                    Feng.success("成功!");
                    table.reload(leaveInfo.tableId);
                } else {
                    Feng.error("失败!" + data.message + "!");
                }
            });
            ajax.set("leaveInfoId", data.leaveInfoId);
            ajax.start();
        };
        Feng.confirm("是否销假?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + leaveInfo.tableId,
        url: Feng.ctxPath + '/leaveLog/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: leaveInfo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        leaveInfo.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        leaveInfo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + leaveInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'detail') {
            leaveInfo.openDetailDlg(data);
        }
        if (layEvent === 'destroy') {
            leaveInfo.onDestroyItem(data);
        }
    });
});
